﻿using System;
using System.Device.Location;

namespace ChinaGeo
{
    public class ChinaGeoModel
    {
        internal bool Cached;
        internal Action<ChinaGeoModel> Effect;
        internal bool IsEarthToChina = true;

        public GeoCoordinate EarthGeo { get; private set; }
        public GeoCoordinate ChinaGeo { get; private set; }
        public DateTimeOffset Timestamp { get; private set; }

        public bool IsEffected { get { return ChinaGeo != null && EarthGeo != null; } }
        public GeoCoordinate EarthToChina { get { return ChinaGeo ?? EarthGeo; } }
        public GeoCoordinate ChinaToEarth { get { return EarthGeo ?? ChinaGeo; } }
        public GeoCoordinate AutoFixed { get { return IsEarthToChina ? EarthToChina : ChinaToEarth; } }

        internal ChinaGeoModel(GeoCoordinate earth, GeoCoordinate china, DateTimeOffset timestamp, bool inside = true)
        {
            EarthGeo = earth;
            ChinaGeo = china;
            Timestamp = !inside || china == null || earth == null ? DateTimeOffset.MinValue : timestamp;
        }

        internal ChinaGeoModel(GeoCoordinate earth, GeoCoordinate china, bool inside = true)
            : this(earth, china, DateTimeOffset.Now, inside) { }

        internal ChinaGeoModel(double earthLatitude, double earthLongitude, double chinaLatitude, double chinaLongitude)
            : this(new GeoCoordinate(earthLatitude, earthLongitude), new GeoCoordinate(chinaLatitude, chinaLongitude)) { }

        internal ChinaGeoModel(double earthLatitude, double earthLongitude, double chinaLatitude, double chinaLongitude, DateTimeOffset timestamp)
            : this(new GeoCoordinate(earthLatitude, earthLongitude), new GeoCoordinate(chinaLatitude, chinaLongitude), timestamp) { }

        internal void UpdateChinaGeo(GeoCoordinate location)
        {
            ChinaGeo = location;
            Timestamp = DateTimeOffset.Now;
        }
    }
}
